package com.zx.www.volunteersystem.controller;

import com.zx.www.volunteersystem.mapper.VolunteerResumeMapper;
import com.zx.www.volunteersystem.pojo.VolunteerResume;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/volunteer-resumes")
public class VolunteerResumeController {

    @Autowired
    private VolunteerResumeMapper volunteerResumeMapper;

    // 添加服务履历记录
    @PostMapping
    public boolean addVolunteerResume(@RequestBody VolunteerResume volunteerResume) {
        try {
            int result = volunteerResumeMapper.insert(volunteerResume);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 根据ID查询服务履历
    @GetMapping("/{resumeId}")
    public VolunteerResume getVolunteerResumeById(@PathVariable Integer resumeId) {
        return volunteerResumeMapper.selectById(resumeId);
    }

    // 根据用户ID查询所有服务履历
    @GetMapping("/user/{userId}")
    public List<VolunteerResume> getVolunteerResumesByUserId(@PathVariable Integer userId) {
        return volunteerResumeMapper.selectByUserId(userId);
    }

    // 根据活动ID查询所有服务履历
    @GetMapping("/activity/{activityId}")
    public List<VolunteerResume> getVolunteerResumesByActivityId(@PathVariable Integer activityId) {
        return volunteerResumeMapper.selectByActivityId(activityId);
    }

    // 获取所有服务履历
    @GetMapping
    public List<VolunteerResume> getAllVolunteerResumes() {
        return volunteerResumeMapper.selectAll();
    }

    // 更新服务履历记录
    @PutMapping("/{resumeId}")
    public boolean updateVolunteerResume(@PathVariable Integer resumeId, @RequestBody VolunteerResume volunteerResume) {
        volunteerResume.setResume_id(resumeId);
        try {
            int result = volunteerResumeMapper.update(volunteerResume);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    // 删除服务履历记录
    @DeleteMapping("/{resumeId}")
    public boolean deleteVolunteerResume(@PathVariable Integer resumeId) {
        try {
            int result = volunteerResumeMapper.deleteById(resumeId);
            return result > 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
